import { graphHelper } from './../../../api/GraphHelper';

class Property {
/**
 * 属性key
 */
  propertyKey:string
  
  /**
 * 目标类型，默认为''，表示对所有类型的graphNode生效，如果有targetTypeName则对特定的类型node生效
 */
  targetTypeName =''
  /**
 * 属性配置,内置属性才有config
 */
  config:any
  
  /**
 * 获取graphNode的属性值
 * @param graphNode
 * @param option
 */
  getValue(graphNode:GraphNode, option?:any):any{
  
  }
  /**
 * 设置graphNode的属性值 
 * @param graphNode
 * @param val 
 * @param option
 */
  setValue(graphNode:GraphNode, val:any, option?:any):any{
    if (!this.validateValue(graphNode, val, option)) return;
    //// 执行设置属性逻辑
    this.afterSetValue(graphNode, val, option);
  }
  
  validateValue(graphNode:GraphNode, val:any, option?:any):boolean{
    return true;
  
  }
  afterSetValue(graphNode:GraphNode, val:any, option?:any):void{
  
  }
  
  getValueType(){
  
  }
  setValueType(){
  
  }
  /**
 * 属性渲染组件
 */
  getComponent(graphNode:GraphNode, propertyKey:string){
    return 'input';
  
    return this.config.editType;
  }
  
}
class PropertyFactory {
  propertyMap:Map<string, Map<string, Property>> = new Map();
  add(prop:Property){
    const keyMap = this.propertyMap.get(prop.propertyKey);
    if (!keyMap){
      this.propertyMap.set(prop.propertyKey, new Map());
    }
    this.propertyMap.get(prop.propertyKey).set(prop.targetTypeName, prop);
  
    //// this.propertyMap.set(prop.propertyKey, prop);
  
  } 
  get(propertyKey:string, typeName = ''){
    const keyMap = this.propertyMap.get(propertyKey);
    if (!keyMap){
      return this.propertyMap.get('stProperty').get('');
    }
    return keyMap.get(typeName) || keyMap.get('');
  
  }
  delete(propertyKey:string, typeName:string){
    this.propertyMap.get(propertyKey).delete(typeName);
  
  }
}
export const propertyFactory = new PropertyFactory();
  
class NameProperty extends Property {
  propertyKey='Name'
  
  getValue(graphNode:GraphNode, option?:any){
    return graphNode.model.name;
  }
  setValue(graphNode:GraphNode, val:any, option?:any){
    graphNode.updateName(val);
  }
}
propertyFactory.add(new NameProperty());
  
class OwnerProperty extends Property {
  propertyKey='Owner'
  config={}
  
  getValue(graphNode:GraphNode, option?:any){
    return graphNode.model.getOwner(); 
  
  }
  setValue(graphNode:GraphNode, newContainer:GraphNode, option?:any){
    graphNode.updateContainer(newContainer);
  }
  //// setValueType(){
  
  //// }
}
  
class TypeProperty extends Property {
  propertyKey='Type'
  config={}
  
  afterSetValue(){
  
  }
  getValue(graphNode:GraphNode, option?:any){
    return graphNode.model.appliedStereotypes;
  
  }
  setValue(graphNode:GraphNode, val:any, option?:any){
  
  }
  
}
  
class WeightProperty extends Property {
  propertyKey='Weight'
  
  getValue(graphNode:GraphNode){
    return graphNode.model.getWeight();
  }
  setValue(graphNode:GraphNode, value:any, option?:any){ //// value: valueSpec
    const curValueSpec  = this.getValue(graphNode);
    if (curValueSpec.valueType === value.valueType){
      curValueSpec.value = value.value;
    } else {
      const oldPropertyNode = graphHelper.getGraphNodeById(graphNode.model.weight, app.activeProjectId);
      oldPropertyNode.remove();
      graphNode.addChildNode('', app.activeProjectId);
      graphNode.model.update(value);

    }

  }
}
propertyFactory.add(new WeightProperty());

class DefaultProperty extends WeightProperty {
  propertyKey='DefaultValue'
  getValue(graphNode:GraphNode){
    return graphNode.model.getDefaultValue();
  }
}
propertyFactory.add(new DefaultProperty());
  
/**
   * 用户自定义的属性
   */
export class CustomProperty extends Property {
  propertyKey="stProperty"
  getComponent(graphNode:GraphNode, propertyKey:string){
  
  }
  getValue(graphNode:GraphNode, option?:any){
  
  }
  setValue(graphNode:GraphNode, val:any, option?:any){
  
  }
}
propertyFactory.add(new CustomProperty());
  
//// 每个模型都能从继承链上回归获得自己有哪些propertyKey
//// 每个PropertyKey都对应一个Property类，这个类描述了要如何获取属性，设置属性，渲染属性
//// 对于用户自定义的属性拿到的key是一个uuid，这个uuid拿到这个属性的定义（是个模型）,根据属性的定义可以生成一份config，